package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.awt.Color;

public class SGIRegisterUI extends SGIClientFrame {

	private JPanel contentPane;
	private JTextField textFieldUsername;
	private JTextField textFieldPassword;
	private JTextField textFieldFname;
	private JTextField textFieldLname;
	private JTextField textFieldPhone;
	private JTextField textFieldEmail;
	private SGIRegisterController regcontroller;
	private JTextField textFieldBalance;
	
	/**
	 * Create the frame.
	 */
	public SGIRegisterUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,455,295);
		setTitle("SGI Register");
		contentPane = new JPanel();
		contentPane.setForeground(Color.RED);
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(28, 33, 102, 14);
		contentPane.add(lblUsername);
		
		textFieldUsername = new JTextField();
		textFieldUsername.setBounds(121, 30, 95, 20);
		contentPane.add(textFieldUsername);
		textFieldUsername.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(235, 33, 89, 14);
		contentPane.add(lblPassword);
		
		textFieldPassword = new JPasswordField();
		textFieldPassword.setBounds(303, 30, 101, 20);
		contentPane.add(textFieldPassword);
		textFieldPassword.setColumns(10);
		
		JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setBounds(28, 71, 102, 14);
		contentPane.add(lblFirstName);
		
		textFieldFname = new JTextField();
		textFieldFname.setBounds(121, 68, 95, 20);
		contentPane.add(textFieldFname);
		textFieldFname.setColumns(10);
		
		JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setBounds(235, 71, 116, 14);
		contentPane.add(lblLastName);
		
		textFieldLname = new JTextField();
		textFieldLname.setBounds(303, 68, 101, 20);
		contentPane.add(textFieldLname);
		textFieldLname.setColumns(10);
		
		JLabel lblMobilePhone = new JLabel("Mobile Phone:");
		lblMobilePhone.setBounds(28, 108, 102, 14);
		contentPane.add(lblMobilePhone);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(235, 108, 102, 14);
		contentPane.add(lblEmail);
		
		textFieldPhone = new JTextField();
		textFieldPhone.setBounds(121, 105, 95, 20);
		contentPane.add(textFieldPhone);
		textFieldPhone.setColumns(10);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(303, 105, 101, 20);
		contentPane.add(textFieldEmail);
		textFieldEmail.setColumns(10);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(textFieldUsername.getText().equals("") || textFieldPassword.getText().equals("") 
						|| textFieldFname.getText().equals("") || textFieldLname.getText().equals("") 
						|| textFieldEmail.getText().equals("") || textFieldPhone.getText().equals("")
						|| textFieldBalance.getText().equals(""))
					JOptionPane.showMessageDialog(SGIRegisterController.getUI(),"You must fill all fileds.");					
				else
				{
					try {
						float balance = Float.valueOf(textFieldBalance.getText());
						if (balance > 999999)
							JOptionPane.showMessageDialog(SGIRegisterController.getUI(),"Balance must be lower than 999,999.");	
						else
						{
							try {
								regcontroller.register(textFieldUsername.getText(),textFieldPassword.getText(),textFieldFname.getText(),textFieldLname.getText(),textFieldEmail.getText(),textFieldPhone.getText(),textFieldBalance.getText());
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}catch (NumberFormatException e) {JOptionPane.showMessageDialog(SGIRegisterController.getUI(),"Balance must be a number.");}
					
				}
			}
		});
		btnRegister.setBounds(282, 181, 122, 40);
		contentPane.add(btnRegister);
		
		JButton btnCancel = new JButton("Close");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				SGIRegisterController.terminate();
				SGIUserAccountMenuController.create(null);
			} 
		});
		btnCancel.setBounds(27, 181, 116, 40);
		contentPane.add(btnCancel);
		
		JLabel lblBalance = new JLabel("Balance:");
		lblBalance.setBounds(28, 143, 46, 14);
		contentPane.add(lblBalance);
		
		textFieldBalance = new JTextField();
		textFieldBalance.setBounds(121, 140, 95, 20);
		contentPane.add(textFieldBalance);
		textFieldBalance.setColumns(10);
	}
	
	public void setRegController(SGIRegisterController regcontroller)
	{
		this.regcontroller = regcontroller;
	}
	
	public void notify(String str)
	{
		JOptionPane.showMessageDialog(this,str);
	}
	
	public void clearAllFields()
	{
		textFieldUsername.setText("");
		textFieldPassword.setText("");
		textFieldFname.setText("");
		textFieldLname.setText("");
		textFieldPhone.setText("");
		textFieldEmail.setText("");
		textFieldBalance.setText("");
	}
}
